//
//  LRXSettingViewController.m
//  路人侠
//
//  Created by 王涛 on 16/5/3.
//  Copyright © 2016年 zhangxulong. All rights reserved.
//

#import "LRXSettingViewController.h"
#import "LRXPayManagerCell.h"
#import "LRXFixPayAcountView.h"
#import "LRXFixPayPasswordView.h"

static NSString * cellID = @"cellREID";
@interface LRXSettingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) LRXFixPayAcountView * acountView;
@property (nonatomic,strong) LRXFixPayPasswordView * passwordView;
@property (nonatomic,strong) UIView * grayView;
@end

@implementation LRXSettingViewController
- (UIView *)grayView{
    if (_grayView == nil) {
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(grayViewClick)];
        _grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MainScreenW, MainScreenH)];
        _grayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.54];
        [_grayView addGestureRecognizer:tap];
    }
    return _grayView;
}
- (LRXFixPayAcountView *)acountView{
    if (_acountView == nil) {
        _acountView = [[LRXFixPayAcountView alloc]init];
       
    }
    CAKeyframeAnimation * bringAni = [CAKeyframeAnimation animation];
    bringAni.keyPath = @"transform.scale";
    bringAni.values = @[@0,@1];
    bringAni.duration = 0.15;
    bringAni.removedOnCompletion=NO;
    bringAni.fillMode=kCAFillModeForwards;
     [_acountView.layer addAnimation:bringAni forKey:nil];
    return _acountView;
}
- (LRXFixPayPasswordView *)passwordView
{
    if (_passwordView == nil) {
        _passwordView = [[LRXFixPayPasswordView alloc]init];
    }
    CAKeyframeAnimation * bringAni = [CAKeyframeAnimation animation];
    bringAni.keyPath = @"transform.scale";
    bringAni.values = @[@0,@1];
    bringAni.duration = 0.15;
    bringAni.removedOnCompletion=NO;
    bringAni.fillMode=kCAFillModeForwards;
    [_passwordView.layer addAnimation:bringAni forKey:nil];
    return _passwordView;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"支付设置";
    UITableView * tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.backgroundColor = [UIColor colorWithRed:244 green:244 blue:244];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LRXPayManagerCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    pushNewViewBlock newViewBlock;
    if (cell == nil) {
        cell = [[LRXPayManagerCell alloc]init];
    }
    if (indexPath.row == 0) {
        cell.cellTitle = @"支付宝账号";
        cell.detailTitle = @"绑定";
        newViewBlock = ^{
            [[UIApplication sharedApplication].keyWindow addSubview:self.grayView];
            [[UIApplication sharedApplication].keyWindow addSubview:self.acountView];
            
        };
    }
    else if(indexPath.row == 1)
    {
        cell.cellTitle = @"支付密码";
        cell.detailTitle = @"设置";
        newViewBlock = ^{
            [[UIApplication sharedApplication].keyWindow addSubview:self.grayView];
            [[UIApplication sharedApplication].keyWindow addSubview:self.passwordView];
        };
    }
    [cell layoutWithBlock:newViewBlock];
    
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LRXPayManagerCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.viewBlock();
}
#pragma mark - grayViewClick 背景点击
-(void)grayViewClick
{
    CAKeyframeAnimation * moreAni = [CAKeyframeAnimation animation];
    moreAni.keyPath = @"transform.scale";
    moreAni.values = @[@1,@0];
    moreAni.duration = 0.15;
    moreAni.removedOnCompletion=NO;
    moreAni.fillMode=kCAFillModeForwards;
    
    [self.acountView.layer addAnimation:moreAni forKey:nil];
    [self.passwordView.layer addAnimation:moreAni forKey:nil];
    
    [self.grayView removeFromSuperview];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.acountView removeFromSuperview];
        [self.passwordView removeFromSuperview];
        
        self.acountView = nil;
        self.passwordView = nil;
    });
    
    
    
}
@end
